package com.cytech.cloudclinic.moudle.main.drug.add.base;

import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.AddDrugTag;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.bean.resp.RespDrugDetail;
import com.yechaoa.yutils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddDrugBasePresenter extends BasePresenter<AddDrugBaseView>{
    public AddDrugBasePresenter(AddDrugBaseView baseView) {
        super(baseView);
    }

    /**
     * 获取药品标签列表
     */
    public void getSummaryList(int modeId,int drugId) {
        JSONObject json = new JSONObject();
        try {
            json.put("mode_id",modeId);
            json.put("drug_id",drugId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.getSummaryList(body), new BaseObserver<BaseBean<List<AddDrugTag>>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<List<AddDrugTag>> o) {
                baseView.getTagSuccess(o.result);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 添加药品标签列表
     */
    public void addSummaryTag(int modeId,int drugId,String value) {
        JSONObject json = new JSONObject();
        try {
            json.put("mode",modeId);
            json.put("drug_tag",drugId);
            json.put("value",value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.addSummaryTag(body), new BaseObserver<BaseBean<Object>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<Object> o) {

            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 删除药品标签
     */
    public void delSummaryTag(int modeId) {
        JSONObject json = new JSONObject();
        try {
            json.put("id",modeId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.delSummaryTag(body), new BaseObserver<BaseBean<Object>>(baseView, true) {

            @Override
            public void onSuccess(BaseBean<Object> o) {
                ToastUtil.showToast("删除成功");
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }

    /**
     * 获取药品详情
     */
    public void getDrugDetail(int drugId) {
        JSONObject json = new JSONObject();
        try {
            json.put("drug_id",drugId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        addDisposable(apiServer.detailDrug(body), new BaseObserver<BaseBean<RespDrugDetail>>(baseView, false) {

            @Override
            public void onSuccess(BaseBean<RespDrugDetail> o) {
                baseView.getDrugDetailSuccess(o.result);
            }

            @Override
            public void onError(String msg) {
                ToastUtil.showToast(msg);
            }
        });
    }
}
